package com.generate.main;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.generate.bean.DBTableBean;
import com.generate.bean.HBMGenBean;
import com.generate.bean.PageBean;
import com.util.CommException;
import com.util.ConfigConts;
import com.util.DataBaseInfo;
import com.util.FileComm;
import com.util.FreemarkerUtil;

public class GenSource {
	private DataBaseInfo dbinfo = new DataBaseInfo();
	
	public void doTableToFLT(String schema,String table){
		System.out.println("--------------------do Gen Table--------------------");
		DataBaseInfo dbinfo = new DataBaseInfo();
//		try {
//			dbinfo.tableList(null);
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		//取表格对象
		DBTableBean tableBean = dbinfo.getTableColumns(schema, table);
		if (tableBean!=null){
			System.out.println("----------rtn the object of "+ tableBean.getTablename() +" ---it's cols:" + 
					tableBean.getColSize() +"--------Primary Key is :"+ tableBean.getPmKey() +"---");
			System.out.println("--All cols is:"+tableBean.getAllColumns());

			System.out.println("----------do generate--------------------------");
			FreemarkerUtil fu = new FreemarkerUtil();
			Map<String,Object> root = new HashMap<String,Object>();
			root.put("tableBean", tableBean);
			String outPath=ConfigConts.CNS_OutputPath+"\\"+tableBean.getTablename();
			try{
				FileComm.createFolder(outPath);
			}catch(CommException e){
				System.out.println("::"+e.getMessage());
			}
			for (String file:ConfigConts.filesList){
				fu.fprint(file, root, outPath+"\\" + file);
				System.out.println("----------generated "+outPath +"\\" + file +", is finished--------------------------");
			}//for
		}else{
			System.out.println("----------get table error!---------------------");
			
		}
		System.out.println("-----------------done finishing--------------------");
	}

	public void doHBMXMLObjToFLT(String XMLFile,String packagePath,String codeName){
		this.doHBMXMLObjToFLT(XMLFile,packagePath,codeName,null);
	}
	
	/**
	 * 通过Hibernate 的XML文件生成代码
	 * @param XMLFile HBM文件
	 */
	public void doHBMXMLObjToFLT(String XMLFile,String packagePath,String codeName,String codeMark){
		XMLExplain xmlEx = new XMLExplain(XMLFile);
		HBMGenBean xmlBean = xmlEx.getHBMBeanFormXML();
		
		if(xmlBean!=null){
			System.out.println("----------rtn the object of "+ xmlBean.getTablename() +" ---it's cols:" + 
					xmlBean.getColSize() +"--------Primary Key is :"+ xmlBean.getPmKey() +"---");
			

			System.out.println("----------do generate--------------------------");
			//页面参数
			PageBean pageSet = new PageBean(codeName,packagePath);
			//代码生成
			FreemarkerUtil fu = new FreemarkerUtil();
			Map<String,Object> root = new HashMap<String,Object>();
			root.put("xmlBean", xmlBean);
			root.put("pageSet", pageSet);
			//转化包路径
			String getfilePckgPath = StringUtils.replaceChars(packagePath, ".", "\\");
			//以传入的packagePath提供
			String fileMark = codeMark;
			if (StringUtils.isEmpty(fileMark)){
				fileMark = xmlBean.getCodeMark();
			}else{
				xmlBean.setCodeMark(fileMark);
			}
			//遍列生成代码
			for (String file:ConfigConts.filesList){
				String filePath = ConfigConts.CNS_FreemarkPath + file;
				List<File> lstFile = FileComm.fGetAllFiles(filePath);
				String baseFolder = ConfigConts.CNS_OutputPath+file+"\\"+getfilePckgPath;
				for(File fltFile:lstFile){
					//目标文件名
					System.out.println("-----------generated: "+ fltFile.getPath());
					//目录路径
//					if(file.indexOf("src"))
					String desPath = StringUtils.replace(fltFile.getParent(),filePath,baseFolder,1)+
						"\\"+StringUtils.capitalize(fileMark)+fltFile.getName();
					
					fu.fprint(fltFile.getPath(), root, desPath);
					
					System.out.println("-------out file path: "+ desPath	+"------------------");
				}
				//生成文件
			}//for
		}else{
			System.out.println("----------get Obecjt error!---------------------");
			
		}
		System.out.println("-----------------done finishing--------------------");
	}
}
